/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.io.Serializable;
import java.sql.Date;
import java.util.*;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.dao.PhongTroDAO;
import model.dao.QuanDAO;
import model.dao.UserDAO;
import model.entity.PhongTro;
import model.entity.Quan;
import model.entity.User;

/**
 *
 * @author duy
 */
//@ManagedBean(name = "phongtroBean", eager = true)
@ManagedBean
@RequestScoped
public class PhongTroBean {

    private PhongTro phongTro;
    private String searchbyUser;
    private final HttpServletRequest request;
    private final FacesContext fc;
    private final HttpSession session;
    private FacesMessage facesMessage;
    private final Map<String, String> params;

    PhongTroDAO PHONGTRO_SERVICE = PhongTroDAO.getInstance();
    QuanDAO QUAN_SERVICE = QuanDAO.getInstance();
    UserDAO USER_SERVICE = UserDAO.getInstance();
   

    public PhongTroBean() {
        fc = FacesContext.getCurrentInstance();
        request = (HttpServletRequest) fc.getExternalContext().getRequest();
        params = fc.getExternalContext().getRequestParameterMap();
        session = request.getSession(true);
        this.phongTro = new PhongTro();
    }
    private List<PhongTro> allPhongTro;

    public List<PhongTro> getAllPhongTro() {
        List<PhongTro> allPhongTro = PHONGTRO_SERVICE.getAllPhongTro();
        return allPhongTro;
    }

    public List<Quan> getAllQuan() {

        return QUAN_SERVICE.getAllQuan();
    }

    public void setAllQuan(List<Quan> allQuan) {
        this.allQuan = allQuan;
    }
    private List<Quan> allQuan;
    private List<User> allUser;

    public List<User> getAllUser() {
        List<User> userList = USER_SERVICE.getAllUser();
        return userList;
    }

    private PhongTro phonTro;
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    private Quan quan;

    public PhongTro getPhongTro() {
        return phongTro;
    }

    public void setPhongTro(PhongTro phongTro) {
        this.phongTro = phongTro;
    }

    public Quan getQuan() {
        return quan;
    }

    public void setQuan(Quan quan) {
        this.quan = quan;
    }
  public String edit(PhongTro phongtro1) {
        this.phongTro = phongtro1;
        return "sua.jsf ";
    }
    public void xoa(int maphongtro) {
        FacesMessage mess;
        try {
            if (PHONGTRO_SERVICE.xoa(maphongtro)) {
                mess = new FacesMessage("Success!");
            } else {
                mess = new FacesMessage("fail!");
            }
            fc.addMessage("result", mess);

        } catch (Exception ex) {

        }
    }
    
     public String test() {
         return "/module/danhsach.jsf?faces-redirect=true";
     }
     
 
     
    public String createPhongTro() {
        int maphong = getPhongTro().getMaphongtro();
        String tieudegioithieu = getPhongTro().getTieudegioithieu();
        String tenchunha = getPhongTro().getTenchunha();
        int sdt = getPhongTro().getSdt();
        String diachi = getPhongTro().getDiachi();
        float danhgia = getPhongTro().getDanhgia();
        int sophong = getPhongTro().getSophong();
        int dientich = getPhongTro().getDientich();
        String ghichu = getPhongTro().getGhichu();
        java.util.Date ngay = getPhongTro().getNgaydang();
        java.sql.Date ngaydang = new java.sql.Date(ngay.getTime());
        int giaphong = getPhongTro().getGiaphong();
        float kinhdo = getPhongTro().getKinhdo();
        float vido = getPhongTro().getVido();
        boolean isActive = getPhongTro().isIsActive();
        
//         User mauser1 = util.Support.getCurrentUser();
//         int b = mauser1.getMauser();
         int a = 1;
        User mauser = USER_SERVICE.getusersbyID(a);
        System.out.println(mauser);
        
        int quan1 = getPhongTro().getQuan().getMaquan();
        Quan maquan = QUAN_SERVICE.getQuanbyMa(quan1);

        PhongTro phongtro = new PhongTro(0, tieudegioithieu, tenchunha, sdt, diachi,
                danhgia, sophong, dientich, giaphong, ghichu, ngaydang, kinhdo,
                vido, true, maquan, mauser);
        if (PHONGTRO_SERVICE.createPhongTro(phongtro)) {
            return "/module/danhsach.jsf?faces-redirect=true";
        } else {
            return "/module/themmoi.jsf?faces-redirect=true";
        }
    }
    
     public String editPhongTro() {
        int maphongtro = getPhongTro().getMaphongtro();
         System.out.println(maphongtro);
        String tieudegioithieu = getPhongTro().getTieudegioithieu();
         System.out.println(tieudegioithieu);
        String tenchunha = getPhongTro().getTenchunha();
         System.out.println(tenchunha);
      
           
        int sdt = getPhongTro().getSdt();
        System.out.println(sdt);
        String diachi = getPhongTro().getDiachi();
           System.out.println(diachi);
        float danhgia = getPhongTro().getDanhgia();
           System.out.println(danhgia);
        int sophong = getPhongTro().getSophong();
         System.out.println(sophong);
        int dientich = getPhongTro().getDientich();
         System.out.println(dientich);
        String ghichu = getPhongTro().getGhichu();
         System.out.println(ghichu);
        java.util.Date ngay = getPhongTro().getNgaydang();
         System.out.println(ngay);
        java.sql.Date ngaydang = new java.sql.Date(ngay.getTime());
        int giaphong = getPhongTro().getGiaphong();
         System.out.println(giaphong);
        float kinhdo = getPhongTro().getKinhdo();
         System.out.println(kinhdo);
        float vido = getPhongTro().getVido();
         System.out.println(vido);
       
//         User mauser1 = util.Support.getCurrentUser();
//         int b = mauser1.getMauser();
         int a = 1;
        User mauser = USER_SERVICE.getusersbyID(a);
        System.out.println(mauser);
        
        int quan1 = getPhongTro().getQuan().getMaquan();
        Quan maquan = QUAN_SERVICE.getQuanbyMa(quan1);

        PhongTro phongtro = new PhongTro(maphongtro, tieudegioithieu, tenchunha, sdt, diachi,
                danhgia, sophong, dientich, giaphong, ghichu, ngaydang, kinhdo,
                vido, true, maquan, mauser);
        if (PHONGTRO_SERVICE.editPhongTro(phongtro)) {
            return "/module/danhsach.jsf?faces-redirect=true";
        } else {
            return "/module/themmoi.jsf?faces-redirect=true";
        }
    }
}
